package com.yzyz.service.impl;

import java.util.List;

import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.yzyz.bean.hotel.HotelBean;
import com.yzyz.dao.IHotelDao;
import com.yzyz.dao.IScenicDao;
import com.yzyz.service.IHotelService;
import com.yzyz.utils.error.Error;

/**
 * 酒店逻辑操作
 * @author david.sun
 * @date 2010-6-9
 */
public class HotelServiceImpl implements IHotelService {

	private IHotelDao hotelDao;
	
	private IScenicDao scenicDao;
	
	public void setScenicDao(IScenicDao scenicDao) {
		this.scenicDao = scenicDao;
	}
	
	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int add(Object object) {
		
		return this.hotelDao.save(object);
	}

	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int delete(int id) {
		if(this.hotelDao.get(id) == null) {
			return Error.CAN_NOT_FIND_OBJECT;
		}
		this.hotelDao.delete(id);
		return Error.SUCCESS;
	}

	@Override
	public Object find(int id) {
		
		return this.hotelDao.get(id);
	}

	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int update(Object object) {
		this.hotelDao.update(object);
		return Error.SUCCESS;
	}

	@Override
	public void setHotelDao(IHotelDao hotelDao) {
		this.hotelDao = hotelDao;
		
	}

	@Override
	public int getCount() {
		
		return this.hotelDao.getCount(-1);
	}

	@Override
	public List<HotelBean> paginate(int start, int limit) {
		
		return this.hotelDao.paginate(start, limit);
	}

	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int saveScenic(int hotelId, int scenicId) {
		if(this.scenicDao.get(scenicId) == null) {
			return Error.SCENIC_NOT_EXIST;
		}
		if(this.hotelDao.get(hotelId) == null) {
			return Error.HOTEL_NOT_EXIST;
		}
		this.hotelDao.saveScenic(hotelId, scenicId);
		return Error.SUCCESS;
		
	}

	@Transactional(propagation = Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public int updateAvailable(int hotelId, boolean enabled) {
		if(this.hotelDao.get(hotelId) == null) {
			return Error.CAN_NOT_FIND_OBJECT;
		}
		this.hotelDao.updateAvailable(hotelId, enabled);
		return Error.SUCCESS;
	}

	@Override
	public List<HotelBean> paginate(int start, int limit, String hotelName,
			String scenicName) {
		
		return this.hotelDao.paginate(start, limit, hotelName, scenicName);
	}

}
